package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.annotation.InterfaceParam;
import cn.dansj.common.utils.bean.CacheToken;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.entity.User;
import cn.dansj.common.utils.enums.ResponseCode;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.common.request.RestTemplateUtils;
import cn.dansj.common.request.RequestUtils;
import cn.dansj.common.utils.transfer.GetTime;
import cn.dansj.common.utils.transfer.Transformation;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.interfaces.InterfaceUtils;
import cn.dansj.utils.project.ProjectUtils;

import java.util.*;

@SuppressWarnings("all")
@MarkAsApi
public class WxloginByCode extends InterfaceHandler {
    @InterfaceParam(value = "code", require = true, comment = "微信用户code")
    @Override
    public Object execute(Map<String, Object> paramsMap) {
        try {
            Object code = paramsMap.get("code");
            String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx21cc4de5168e41c4&secret=078163ccf888c6800ebdf2705a82d564&js_code=" + code + "&grant_type=authorization_code";
            String openid = DictMap.parseObject(RestTemplateUtils.defaultInstance().get(url, String.class)).getString("openid");
            User user = DictMap.parseObject(jdbcTemplate.queryForMap("select * from x_user_info where open_id=?", openid), User.class);
            RequestUtils.getRequest().setAttribute("currentUsername", user == null ? "" : user.getUsername());

            if (user == null || Verification.checkNull(user.getOpenId())) {
                throw new InterfaceResponseException(ResponseCode.AuthenticationFailed.getStatusCode(), "当前微信未绑定用户,请注册或登录后绑定");
            } else if (!"1".equals(user.getStatus())) {
                throw new InterfaceResponseException(ResponseCode.AuthenticationFailed.getStatusCode(), "当前用户禁止登陆");
            } else if (user.getLoginFrom() == 2) {
                throw new InterfaceResponseException(ResponseCode.AuthenticationFailed.getStatusCode(), "当前用户手机端无法登录");
            } else {
                CacheToken cacheToken = ProjectUtils.createToken(user);
                DictMap userInfo = DictMap.of();
                userInfo.put("username", user.getUsername());
                userInfo.put("nickname", user.getNickName());
                userInfo.put("avatar", user.getAvatar());
                userInfo.put("role", user.getRole());
                userInfo.put("token", cacheToken.getUuid());
                userInfo.put("bind_wx", Transformation.castToBoolean(user.getOpenId()));
                userInfo.put("mobile", user.getMobile());
                userInfo.put("mail", user.getMail());
                userInfo.put("sequence", user.getSequence());
                userInfo.put("point", user.getPoint());
                userInfo.put("pay_count", user.getPayCount());
                userInfo.put("crt_time", GetTime.stringTime(user.getCrtTime()));
                userInfo.put("api", user.getToken());
                return ProjectUtils.encryptByPrivateKey(userInfo.toJSONString());
            }
        } catch (InterfaceResponseException interfaceResponseException) {
            throw interfaceResponseException;
        } catch (Exception exception) {
            InterfaceUtils.logger.error("error:", exception);
            throw new InterfaceResponseException(ResponseCode.AuthenticationFailed);
        }
    }
}